package cn.com.bod.fas.pm.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import cn.com.bod.fas.pm.dao.mapper.CuCompanyLegalContacterMapper;
import cn.com.bod.fas.pm.dao.mapper.CuCorpCustBaseMapper;
import cn.com.bod.fas.pm.dao.mapper.CuCustomerMapper;
import cn.com.bod.fas.pm.model.po.CuCustomer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.bod.fas.pm.api.model.vo.dto.request.FeignCorpCustRequVo;
import cn.com.bod.fas.pm.api.model.vo.dto.request.FeignCustBaseRequVo;
import cn.com.bod.fas.pm.api.service.FeignCuCorpCustBaseService;
import cn.com.bod.fas.pm.model.po.CuCompanyLegalContacter;
import cn.com.bod.fas.pm.model.po.CuCorpCustBase;
import cn.com.bod.fas.pm.utils.DemoUtil;
import cn.com.yusys.yusp.commons.mapper.CommonMapper;
import cn.com.yusys.yusp.commons.service.CommonService;

/**
 * @description 企业客户信息表Service
 * @author houyaoxun
 * @date 2019年9月24日
 * @version 1.0.0
 * @Copyright (c) scfhdt
 */
@Service
public class CuCorpCustBaseServiceImpl extends CommonService implements FeignCuCorpCustBaseService {
	/*
	 * 企业客户基本信息Mapper
	 */
	@Autowired
	private CuCorpCustBaseMapper cuCorpCustBaseMapper;

	/*
	 * 客户信息Mapper
	 */
	@Autowired
	private CuCustomerMapper cuCustomerMapper;
	/*
	 * 企业法人关系人Mapper
	 */
	@Autowired
	private CuCompanyLegalContacterMapper cuCompanyLegalContacterMapper;

	@Override
	protected CommonMapper<?> getMapper() {
		return cuCorpCustBaseMapper;
	}

	/**
	 * @description 新增企业客户
	 * @author houyaoxun
	 * @date 2019年9月18日
	 * @param cuCustomer
	 *            客户信息
	 * @param cuCorpCustBase
	 *            企业客户基本信息
	 * @param cuCorpSurvey
	 *            企业概况信息
	 * @return 客户编号
	 */
	@Override
	@Transactional(rollbackFor = { Exception.class })
	public String addCorpCust(FeignCustBaseRequVo feignCustBaseRequVo) {

		CuCustomer cuCustomer = DemoUtil.beanCopy(feignCustBaseRequVo, CuCustomer::new);

		CuCorpCustBase cuCorpCustBase = new CuCorpCustBase();

		/*
		 * 生成客户号
		 */
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyMMddHHmmss");
		String custNo = sdf.format(date);
		cuCustomer.setCustNo(custNo);
		cuCorpCustBase.setCustNo(custNo);
		/*
		 * 新增企业客户基本信息
		 */
		cuCorpCustBaseMapper.insertSelective(cuCorpCustBase);
		/*
		 * 新增客户信息
		 */
		cuCustomerMapper.insertSelective(cuCustomer);
		return custNo;
	}

	/**
	 * @description 新增企业客户信息详情
	 * @author houyaoxun
	 * @date 2019年9月18日
	 * @param cuCustomer 客户信息
	 * @param cuCorpCustBase 企业客户基本信息
	 * @param cuCorpSurvey 企业概况信息
	 * @return void
	 */

	@Override
	@Transactional(rollbackFor = { Exception.class })
	public void addCorpCustBase(FeignCorpCustRequVo feignCorpCustRequVo) {
		CuCorpCustBase cuCorpCustBase = DemoUtil.beanCopy(feignCorpCustRequVo.getFeignCuCorpCustBaseRequVo(),
				CuCorpCustBase::new);

		/*
		 * 新增企业客户基本信息
		 */
		cuCorpCustBaseMapper.updateByPrimaryKeySelective(cuCorpCustBase);
		
		/*
		 * 生成公司法人联系人信息实体类集合
		 */
		List<CuCompanyLegalContacter> CuCompanyLegalContacters = DemoUtil.listCopy(feignCorpCustRequVo.getFeignCuCorpContactsRequVos(), CuCompanyLegalContacter:: new);
		for (CuCompanyLegalContacter CuCompanyLegalContacter: CuCompanyLegalContacters) {
			cuCompanyLegalContacterMapper.insertSelective(CuCompanyLegalContacter);
		}
	}

}
